const scssFiles = './miniprogram/**/*.scss';
const baseDir = './miniprogram/';
const gulp = require('gulp');
const sass = require('gulp-sass');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
// const autoprefixer = require('gulp-autoprefixer');
const stripCssComments = require('gulp-strip-css-comments');
// Sass 编译

const trimReg = /\n(\n)*( )*(\n)*\n/g;
gulp.task('sass', function () {
    gulp.src(scssFiles)
        .pipe(sass({errLogToConsole: true, outputStyle: 'expanded'})
            .on('error', sass.logError))
        .pipe(rename({
            'extname': '.wxss'
        }))
        .pipe(stripCssComments({
            trim: true,
            space: true
        }))
        .pipe(replace('.scss', '.wxss'))
        .pipe(replace('px', 'rpx'))
        .pipe(replace(trimReg, '\n'))
        .pipe(gulp.dest(baseDir))
});

//在命令行执行：gulp sass:watch，就可实现监听文件变化来自动编译
gulp.task('default', function () {
    gulp.watch(scssFiles, ['sass']);
});